package com.javasea.netty.har.common.utils;

import com.javasea.netty.har.common.entity.Heart;
import com.javasea.netty.har.common.entity.NioClient;
import com.javasea.netty.har.common.entity.Result;
import com.javasea.netty.har.common.entity.print.*;
import com.javasea.netty.har.common.entity.proto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 *@ClassName Converter
 *@Description 将java对象转换为对应的protobuf对象
 *@Author longxiaonan@163.com
 *@Date 2021/6/6 0006 21:24
 */
@Slf4j
public class Converter {

    public static CmdBatchProto.CmdBatch pojo2Protobuf(CmdBatch cmdBatch) {
        try {
            CmdBatchProto.CmdBatch.Builder cmdBatchBuilder = CmdBatchProto.CmdBatch.newBuilder();
            ProtoBeanUtils.toProtoBean(cmdBatch, cmdBatchBuilder);
//            List<Command> commandList = cmdBatch.getCommandList();
//            List<CommandProto.Command> commandProtoList = Lists.newArrayList();
//            for (Command command : commandList) {
//                try {
//                    CommandProto.Command.Builder commandBuilder = CommandProto.Command.newBuilder();
//                    ProtoBeanUtils.toProtoBean(command, commandBuilder);
//                    commandProtoList.add(commandBuilder.build());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//            cmdBatchBuilder.addAllCommandList(commandProtoList);
            return cmdBatchBuilder.build();
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static CmdBatch protobuf2Pojo(CmdBatchProto.CmdBatch cmdBatchProto) {
        try {
            CmdBatch cmdBatch = ProtoBeanUtils.toPojoBean(cmdBatchProto, CmdBatch.class);
            List<Command> commandList = cmdBatch.getCommandList();
            for (Command command : commandList) {
                solveChineseGarbled(command);
            }
//            List<CommandProto.Command> commandListList = cmdBatchProto.getCommandListList();
//            List<Command> commandList = Lists.newArrayList();
//            for (CommandProto.Command commandProto : commandListList) {
//                CommandProto.PrintParam paramProto = commandProto.getParam();
//
//                PrintParam printParam = ProtoBeanUtils.toPojoBean(paramProto, PrintParam.class);
//                // pojo 解决中文乱码
//                solveChineseGarbled(printParam);
//                Command command = ProtoBeanUtils.toPojoBean(commandProto, Command.class);
//                command.setParam(printParam);
//                commandList.add(command);
//            }
//            cmdBatch.setCommandList(commandList);
            return cmdBatch;
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    /** 解决中文乱码问题
     * @param command
     * @throws UnsupportedEncodingException
     */
    private static void solveChineseGarbled(Command command) throws UnsupportedEncodingException {
        PrintParam printParam = command.getParam();
        // 二维码不需要解码，否则url存在问题
        String qrcodeContent = printParam.getQrcodeContent();
        // 需要直接显示的需要解码显示为中文
        if(!qrcodeContent.contains("http")) {
            printParam.setQrcodeContent(decode(qrcodeContent));
        }
        printParam.setCompany((printParam.getCompany()));
        printParam.setName((printParam.getName()));
        printParam.setTheme((printParam.getTheme()));
        printParam.setBianhao((printParam.getBianhao()));
        printParam.setBianhaoBar((printParam.getBianhaoBar()));
        printParam.setGuige(printParam.getGuige());
        printParam.setDes((printParam.getDes()));
        printParam.setPrinterModel((printParam.getPrinterModel()));
        printParam.setProject((printParam.getProject()));
        printParam.setBuild((printParam.getBuild()));
        printParam.setWarehouse((printParam.getWarehouse()));
        printParam.setPosition((printParam.getPosition()));
        printParam.setAluminumCode((printParam.getAluminumCode()));
        printParam.setAluminumType((printParam.getAluminumType()));
        printParam.setPartition((printParam.getPartition()));
        printParam.setInstallation((printParam.getInstallation()));
        printParam.setAddress((printParam.getAddress()));
        printParam.setProduceDate((printParam.getProduceDate()));
    }

    private static String decode(String str) throws UnsupportedEncodingException {
        if(!StringUtils.isEmpty(str)){
            return URLDecoder.decode(str, "utf-8");
        }
        return "";
    }

    public static CommandProto.Command pojo2Protobuf(Command command) {
        try {
            CommandProto.Command.Builder commandBuilder = CommandProto.Command.newBuilder();
            ProtoBeanUtils.toProtoBean(command, commandBuilder);
            return commandBuilder.build();
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static Command protobuf2Pojo(CommandProto.Command commandProto) {
        try {
            Command command = ProtoBeanUtils.toPojoBean(commandProto, Command.class);
            return command;
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static HeartProto.Heart pojo2Protobuf(Heart heart) {
        try {
            HeartProto.Heart.Builder heartBuilder = HeartProto.Heart.newBuilder();
            ProtoBeanUtils.toProtoBean(heart, heartBuilder);
            return heartBuilder.build();
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static Heart protobuf2Pojo(HeartProto.Heart heartProto) {
        try {
            Heart heart = ProtoBeanUtils.toPojoBean(heartProto, Heart.class);
            return heart;
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static PrintStateMsgProto.PrintStateMsgNotice pojo2Protobuf(PrintStateMsgNotice msgNotice) {
        try {
            PrintStateMsgProto.PrintStateMsgNotice.Builder printStateMsgNoticeBuilder = PrintStateMsgProto.PrintStateMsgNotice.newBuilder();
            ProtoBeanUtils.toProtoBean(msgNotice, printStateMsgNoticeBuilder);
            return printStateMsgNoticeBuilder.build();
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static PrintStateMsgNotice protobuf2Pojo(PrintStateMsgProto.PrintStateMsgNotice msgNoticeProto) {
        try {
            return ProtoBeanUtils.toPojoBean(msgNoticeProto, PrintStateMsgNotice.class);
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static PrintStateMsgProto.PrintStateMsgConfirm pojo2Protobuf(PrintStateMsgConfirm msgConfirm) {
        try {
            PrintStateMsgProto.PrintStateMsgConfirm.Builder printStateMsgConfirmBuilder = PrintStateMsgProto.PrintStateMsgConfirm.newBuilder();
            ProtoBeanUtils.toProtoBean(msgConfirm, printStateMsgConfirmBuilder);
            return printStateMsgConfirmBuilder.build();
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static PrintStateMsgConfirm protobuf2Pojo(PrintStateMsgProto.PrintStateMsgConfirm msgConfirmProto) {
        try {
            return ProtoBeanUtils.toPojoBean(msgConfirmProto, PrintStateMsgConfirm.class);
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static NioClientProto.NioClient pojo2Protobuf(NioClient nioClient) {
        try {
            NioClientProto.NioClient.Builder nioClientBuilder = NioClientProto.NioClient.newBuilder();
            ProtoBeanUtils.toProtoBean(nioClient, nioClientBuilder);
            return nioClientBuilder.build();
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static NioClient protobuf2Pojo(NioClientProto.NioClient nioClientProto) {
        try {
            return ProtoBeanUtils.toPojoBean(nioClientProto, NioClient.class);
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static ResultProto.Result pojo2Protobuf(Result result) {
        try {
            ResultProto.Result.Builder builder = ResultProto.Result.newBuilder();
            ProtoBeanUtils.toProtoBean(result, builder);
            return builder.build();
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }

    public static Result protobuf2Pojo(ResultProto.Result resultProto) {
        try {
            return ProtoBeanUtils.toPojoBean(resultProto, Result.class);
        } catch (Exception e) {
            log.error("proto对象转换异常", e);
            throw new RuntimeException("proto对象转换异常");
        }
    }
}
